<?php
/**
 * ZOOLU - Content Management System
 * Copyright (c) 2008-2009 HID GmbH (http://www.hid.ag)
 *
 * LICENSE
 *
 * This file is part of ZOOLU.
 *
 * ZOOLU is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ZOOLU is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ZOOLU. If not, see http://www.gnu.org/licenses/gpl-3.0.html.
 *
 * For further information visit our website www.getzoolu.org 
 * or contact us at zoolu@getzoolu.org
 *
 * @category   ZOOLU
 * @package    library.massiveart.generic
 * @copyright  Copyright (c) 2008-2009 HID GmbH (http://www.hid.ag)
 * @license    http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, Version 3
 * @version    $Id: version.php
 */

/**
 * GenericSetup
 *
 * Version history (please keep backward compatible):
 * 1.0, 2009-01-20: Thomas Schedler
 * 1.1, 2009-07-29: Florian Mathis, added fieldtypes to database
 *
 * @author Thomas Schedler <tsh@massiveart.com>
 * @version 1.0
 * @package massiveart.generic
 * @subpackage GenericSetup
 */

require_once(dirname(__FILE__).'/elements/generic.element.tab.class.php');
require_once(dirname(__FILE__).'/elements/generic.element.region.class.php');
require_once(dirname(__FILE__).'/elements/generic.element.field.class.php');

class GenericSetup {

	protected $intGenFormId;
	protected $intActionType;
	protected $strFormId;
	protected $intTemplateId;
	protected $intFormVersion = null;
	protected $strFormTitle;
	protected $intFormTypeId;
	protected $strFormType;
	protected $intFormLanguageId;
	protected $intWidgetInstanceId;
	protected $strSubwidgetId;

	protected $intRegionId;
	protected $strRegionTitle;
	protected $intRegionCols;
	protected $intRegionPosition;
	protected $blnRegionCollapsable = true;

	protected $intLanguageId;
	protected $intElementId;

	protected $intParentId;
	protected $intParentTypeId;
	protected $intRootLevelId;
	protected $intRootLevelTypeId;
  protected $intElementTypeId;
  protected $blnIsStartPage;
  protected $blnShowInNavigation;
  protected $intUrlFolder;

  protected $strModelSubPath;

  protected $arrTabs = array();
  /**
   * property of the tabs array
   * @return Array $arrTabs
   */
  public function Tabs(){
    return $this->arrTabs;
  }

	protected $arrRegions = array();
  /**
   * property of the regions array
   * @return Array $arrRegions
   */
  public function Regions(){
    return $this->arrRegions;
  }

  protected $arrMultiplyRegionIds = array();
  /**
   * property of the multiply region ids array
   * @return Array $arrMultiplyRegionIds
   */
  public function MultiplyRegionIds(){
    return $this->arrMultiplyRegionIds;
  }

  protected $arrFieldNames = array();
  /**
   * property of the field name array
   * @return Array $arrFieldNames
   */
  public function FieldNames(){
    return $this->arrFieldNames;
  }

	protected $arrCoreFields = array();
  /**
   * property of the core fields array
   * @return Array $arrCoreFields
   */
  public function CoreFields(){
    return $this->arrCoreFields;
  }

	protected $arrFileFields = array();
  /**
   * property of the file fields array
   * @return Array $arrFileFields
   */
  public function FileFields(){
    return $this->arrFileFields;
  }

  protected $arrMultiFields = array();
  /**
   * property of the multi fields array
   * @return Array $arrMultiFields
   */
  public function MultiFields(){
    return $this->arrMultiFields;
  }

	protected $arrInstanceFields = array();
  /**
   * property of the instance fields array
   * @return Array $arrInstanceFields
   */
  public function InstanceFields(){
    return $this->arrInstanceFields;
  }

  protected $arrSpecialFields = array();
  /**
   * property of the special fields array
   * @return Array $arrSpecialFields
   */
  public function SpecialFields(){
    return $this->arrSpecialFields;
  }

	protected $intCreatorId;
	protected $strPublisherName;
	protected $strChangeUserName;
	protected $strPublishDate;
	protected $strChangeDate;
	protected $objPublishDate;
	protected $objChangeDate;
	protected $intStatusId;

	const IS_CORE_FIELD = 'isCoreField';
	const IS_GENERIC_SAVE_FIELD = 'isGenericSaveField';
	const DEFAULT_SORT_POSITION = 999999;

	/**
	 * form types
	 */
	const TYPE_FOLDER = 1;
  const TYPE_PAGE = 2;
  const TYPE_CATEGORY = 3;
  const TYPE_UNIT = 4;
  const TYPE_CONTACT = 5;
  const TYPE_WIDGET = 6;
  const TYPE_SUBWIDGET = 7; 

  /**
   * field type container
   */
  const CORE_FIELD = 1;
  const SPECIAL_FIELD = 2;
  const FILE_FIELD = 3;
  const MULTI_FIELD = 4;
  const INSTANCE_FIELD = 5;

  /**
   * field types constants
   */
	const FIELD_TYPE_TEMPLATE = 'template';
  const FIELD_TYPE_TEXTEDITOR = 'texteditor';
  const FIELD_TYPE_INTERNALLINK = 'internalLink';
  
	/*
   * FieldTypeGroups
   */
  const FIELD_TYPE_FILE_ID = 1;
  const FIELD_TYPE_SELECT_ID = 2;
  const FIELD_TYPE_MULTIFIELD_ID = 3;
  const FIELD_TYPE_SPECIALFIELD_ID = 4;

	/**
	 * @var Core
	 */
	protected $core;

	/**
	 * @var Model_GenericForms
	 */
	protected $objModelGenericForm;

	/**
	 * @var Model_Templates
	 */
	protected $objModelTemplates;

	/**
	 * Constructor
	 */
	public function __construct(){
		$this->core = Zend_Registry::get('Core');
	}

  /**
   * loadGenericForm
   * @author Thomas Schedler <tsh@massiveart.com>
   * @version 1.0
   */
  public function loadGenericForm(){
    try{
      $this->getModelGenericForm();

      /**
       * @var Zend_Db_Table_Rowset
       */
      $objFormData = $this->objModelGenericForm->loadForm($this->strFormId, $this->intActionType, $this->intFormVersion);

      if(count($objFormData) == 1){
        $objForm = $objFormData->current();

        /**
         * set values of the row
         */
        $this->setGenFormId($objForm->id);
        $this->setFormTitle($objForm->title);
        $this->setFormVersion($objForm->version);
        $this->setFormTypeId($objForm->idGenericFormTypes);
        $this->setFormType($objForm->typeTitle);

      }else{
        throw new Exception('Not able to load form!');
      }
    }catch (Exception $exc) {
      $this->core->logger->err($exc);
    }
  }

  /**
   * resetGenericStructure
   * @author Thomas Schedler <tsh@massiveart.com>
   * @version 1.0
   */
  public function resetGenericStructure(){
    $this->core->logger->debug('massiveart->generic->GenericSetup->resetGenericStructure()');

    $this->arrRegions = array();
    $this->arrMultiplyRegionIds = array();
    $this->arrCoreFields = array();
    $this->arrFileFields = array();
    $this->arrInstanceFields = array();
    $this->arrMultiFields = array();
    $this->arrSpecialFields = array();
  }

	/**
	 * loadGenericFormStructure
	 * @author Thomas Schedler <tsh@massiveart.com>
	 * @version 1.0
	 */
	public function loadGenericFormStructure(){
		$this->core->logger->debug('massiveart->generic->GenericSetup->loadGenericFormStructure()');
		try{

			/**
			 * load the generic form model
			 */
			$this->getModelGenericForm();

			$blnCacheGenForm = ($this->core->sysConfig->cache->generic_form_structure == 'true') ? true : false;

			$arrFrontendOptions = array(
			   'caching' => $blnCacheGenForm,
         'lifetime' => null, // cache lifetime (in seconds), if set to null, the cache is valid forever.
         'automatic_serialization' => true
      );

      $arrBackendOptions = array(
          'cache_dir' => GLOBAL_ROOT_PATH.'/tmp/cache/gen_forms/' // Directory where to put the cache files
      );

      // getting a Zend_Cache_Core object
      $objCache = Zend_Cache::factory('Core',
                                      'File',
                                      $arrFrontendOptions,
                                      $arrBackendOptions);

      // see if a cache already exists:
      if(!$objFieldsAndRegionsData = $objCache->load('GenForm'.$this->intGenFormId)) {

        // cache miss; get generic form structure
        $objFieldsAndRegionsData = $this->objModelGenericForm->loadFieldsAndRegionsByFormId($this->intGenFormId);

        $objCache->save($objFieldsAndRegionsData, 'GenForm'.$this->intGenFormId);
      }

		  $arrExcludedRegions = array();
      if($this->getTemplateId() > 0){
        $objTemplateExcludedRegionData = $this->getModelTemplates()->loadTemplateExcludedRegions($this->getTemplateId());
        if(count($objTemplateExcludedRegionData) > 0){
          foreach($objTemplateExcludedRegionData as $objTemplateExcludedRegion){
            $arrExcludedRegions[] = $objTemplateExcludedRegion->idRegions;
          }
        }
      }

      $arrExcludedFields = array();
      if($this->getTemplateId() > 0){
        $objTemplateExcludedFieldData = $this->getModelTemplates()->loadTemplateExcludedFields($this->getTemplateId());
        if(count($objTemplateExcludedFieldData) > 0){
          foreach($objTemplateExcludedFieldData as $objTemplateExcludedField){
            $arrExcludedFields[] = $objTemplateExcludedField->idFields;
          }
        }
      }

		  $arrTemplateRegionProperties = array();
      if($this->getTemplateId() > 0){
        $objTemplateRegionPropertiesData = $this->getModelTemplates()->loadTemplateRegionProperties($this->getTemplateId());
        if(count($objTemplateRegionPropertiesData) > 0){
          foreach($objTemplateRegionPropertiesData as $objTemplateRegionProperty){
            $arrTemplateRegionProperties[$objTemplateRegionProperty->idRegions] = array('order'       => $objTemplateRegionProperty->order,
                                                                                        'collapsable' => $objTemplateRegionProperty->collapsable,
                                                                                        'isCollapsed' => $objTemplateRegionProperty->isCollapsed);
          }
        }
      }

			/**
			 * go through the fields and regions to prepare the generic structure
			 */
			foreach ($objFieldsAndRegionsData as $objFieldRegionTagData) {

			  if(!array_key_exists($objFieldRegionTagData->tabId, $this->arrTabs)){
          $objGenTab = new GenericElementTab();
          $objGenTab->setTabId($objFieldRegionTagData->tabId);
          $objGenTab->setTabTitle($objFieldRegionTagData->tabTitle);
          $objGenTab->setTabOrder($objFieldRegionTagData->tabOrder);
          $this->arrTabs[$objFieldRegionTagData->tabId] = $objGenTab;
			  }

  			if(!in_array($objFieldRegionTagData->regionId, $arrExcludedRegions)){
  				if(!in_array($objFieldRegionTagData->id, $arrExcludedFields)){

	  			  if(!array_key_exists($objFieldRegionTagData->regionId, $this->arrRegions)){

	  			  	$objGenRegion = new GenericElementRegion();
	  			    $objGenRegion->setRegionId($objFieldRegionTagData->regionId);
	            $objGenRegion->setRegionTitle($objFieldRegionTagData->regionTitle);
	            $objGenRegion->setRegionCols($objFieldRegionTagData->regionColumns);
	            if(array_key_exists($objFieldRegionTagData->regionId, $arrTemplateRegionProperties)){
	            	if(!is_null($arrTemplateRegionProperties[$objFieldRegionTagData->regionId]['order']) || $arrTemplateRegionProperties[$objFieldRegionTagData->regionId]['order'] != '')
	            	  $objGenRegion->setRegionOrder($arrTemplateRegionProperties[$objFieldRegionTagData->regionId]['order']);
	            	else
	            	  $objGenRegion->setRegionOrder($objFieldRegionTagData->regionOrder);

	            	if(!is_null($arrTemplateRegionProperties[$objFieldRegionTagData->regionId]['collapsable']) || $arrTemplateRegionProperties[$objFieldRegionTagData->regionId]['collapsable'] != '')
                  $objGenRegion->setRegionCollapsable($arrTemplateRegionProperties[$objFieldRegionTagData->regionId]['collapsable']);
                else
                  $objGenRegion->setRegionCollapsable($objFieldRegionTagData->collapsable);

                if(!is_null($arrTemplateRegionProperties[$objFieldRegionTagData->regionId]['isCollapsed']) || $arrTemplateRegionProperties[$objFieldRegionTagData->regionId]['isCollapsed'] != '')
                  $objGenRegion->setRegionIsCollapsed($arrTemplateRegionProperties[$objFieldRegionTagData->regionId]['isCollapsed']);
                else
                  $objGenRegion->setRegionIsCollapsed($objFieldRegionTagData->isCollapsed);
	            }else{
	              $objGenRegion->setRegionOrder($objFieldRegionTagData->regionOrder);
	              $objGenRegion->setRegionCollapsable($objFieldRegionTagData->collapsable);
                $objGenRegion->setRegionIsCollapsed($objFieldRegionTagData->isCollapsed);
	            }
	            $objGenRegion->setRegionTypeId($objFieldRegionTagData->idRegionTypes);
	            $objGenRegion->setRegionPosition($objFieldRegionTagData->position);
	            $objGenRegion->setRegionIsMultiply($objFieldRegionTagData->isMultiply);
	            $objGenRegion->setRegionMultiplyRegion($objFieldRegionTagData->multiplyRegion);
	            $this->arrRegions[$objFieldRegionTagData->regionId] = $objGenRegion;

	            if($objGenRegion->getRegionIsMultiply() == true){
	              $this->arrMultiplyRegionIds[] = $objFieldRegionTagData->regionId;
	            }

	            $this->getTab($objFieldRegionTagData->tabId)->addRegion($objGenRegion);
	  			  }

	  			  $objGenField = new GenericElementField();
	          $objGenField->id = $objFieldRegionTagData->id;
	          $objGenField->title = $objFieldRegionTagData->title;
	          $objGenField->name = $objFieldRegionTagData->name;
	          $objGenField->typeId = $objFieldRegionTagData->idFieldTypes;
	          $objGenField->type = $objFieldRegionTagData->type;
	          $objGenField->defaultValue = $objFieldRegionTagData->defaultValue;
	          $objGenField->sqlSelect = $objFieldRegionTagData->sqlSelect;
	          $objGenField->columns = $objFieldRegionTagData->columns;
	          $objGenField->order = $objFieldRegionTagData->order;
	          $objGenField->isCoreField = $objFieldRegionTagData->isCoreField;
	          $objGenField->isKeyField = $objFieldRegionTagData->isKeyField;
	          $objGenField->isSaveField = $objFieldRegionTagData->isSaveField;
	          $objGenField->isRegionTitle = $objFieldRegionTagData->isRegionTitle;
	          $objGenField->isDependentOn = $objFieldRegionTagData->isDependentOn;
	          $objGenField->copyValue = $objFieldRegionTagData->copyValue;
	          $objGenField->decorator = $objFieldRegionTagData->decorator;
	          $objGenField->isMultiply = $objFieldRegionTagData->isMultiply;
	          $objGenField->idSearchFieldTypes = $objFieldRegionTagData->idSearchFieldTypes;
						$objGenField->idFieldTypeGroup = $objFieldRegionTagData->idFieldTypeGroup;
						$objGenField->isSqlKeyValuePair = $objFieldRegionTagData->isSqlKeyValuePair;
						
	  			  /**
	           * select field container
	           */
  				if($objGenField->isSaveField == 1){
	            if($objGenField->isMultiply == 1){
	            	if($objGenField->idFieldTypeGroup == GenericSetup::FIELD_TYPE_SPECIALFIELD_ID) {
	                $this->getRegion($objFieldRegionTagData->regionId)->addSpecialFieldName($objGenField->name);
	              }else if($objGenField->idFieldTypeGroup == GenericSetup::FIELD_TYPE_FILE_ID){
	                $this->getRegion($objFieldRegionTagData->regionId)->addFileFieldName($objGenField->name);
	              }else if($objGenField->idFieldTypeGroup == GenericSetup::FIELD_TYPE_MULTIFIELD_ID){
	                $this->getRegion($objFieldRegionTagData->regionId)->addMultiFieldName($objGenField->name);
	              }else{
	                $this->getRegion($objFieldRegionTagData->regionId)->addInstanceFieldName($objGenField->name);
	              }
	            }else{
	              if($objGenField->isCoreField == 1){
	                $this->arrCoreFields[$objGenField->name] = $objGenField;
	                $this->arrFieldNames[$objGenField->name] = self::CORE_FIELD;
	              }else if($objGenField->idFieldTypeGroup == GenericSetup::FIELD_TYPE_SPECIALFIELD_ID){
	                $this->arrSpecialFields[$objGenField->name] = $objGenField;
	                $this->arrFieldNames[$objGenField->name] = self::SPECIAL_FIELD;
	              }else if($objGenField->idFieldTypeGroup == GenericSetup::FIELD_TYPE_FILE_ID){
	                $this->arrFileFields[$objGenField->name] = $objGenField;
	                $this->arrFieldNames[$objGenField->name] = self::FILE_FIELD;
	              }else if($objGenField->idFieldTypeGroup == GenericSetup::FIELD_TYPE_MULTIFIELD_ID){
	                $this->arrMultiFields[$objGenField->name] = $objGenField;
	                $this->arrFieldNames[$objGenField->name] = self::MULTI_FIELD;
	              }else{
	                $this->arrInstanceFields[$objGenField->name] = $objGenField;
	                $this->arrFieldNames[$objGenField->name] = self::INSTANCE_FIELD;
	              }
	            }
	          }

            $this->getRegion($objFieldRegionTagData->regionId)->addField($objGenField);
  				}
  		  }
			}

		}catch (Exception $exc) {
			$this->core->logger->err($exc);
		}
	}

	/**
   * getTab
   * @param integer $intTabId
   * @return GenericElementTab
   * @author Thomas Schedler <tsh@massiveart.com>
   * @version 1.0
   */
  public function getTab($intTabId){
    if(array_key_exists($intTabId, $this->arrTabs)){
      return $this->arrTabs[$intTabId];
    }
    return null;
  }

	/**
   * getRegion
   * @param integer $intRegionId
   * @return GenericElementRegion
   * @author Thomas Schedler <tsh@massiveart.com>
   * @version 1.0
   */
	public function getRegion($intRegionId){
		if(array_key_exists($intRegionId, $this->arrRegions)){
	  	return $this->arrRegions[$intRegionId];
	  }
	  return null;
	}

  /**
   * getField
   * @param string $strField
   * @return GenericElementField
   * @author Cornelius Hansjakob <cha@massiveart.com>
   * @version 1.0
   */
  public function getField($strField){
    if(array_key_exists($strField, $this->arrFieldNames)){
      switch($this->arrFieldNames[$strField]){
      	case self::CORE_FIELD:
      		return $this->getCoreField($strField);
      		break;
      	case self::SPECIAL_FIELD:
          return $this->getSpecialField($strField);
          break;
        case self::FILE_FIELD:
          return $this->getFileField($strField);
          break;
        case self::MULTI_FIELD:
          return $this->getMultiField($strField);
          break;
        case self::INSTANCE_FIELD:
          return $this->getInstanceField($strField);
          break;
      }
    }
    return null;
  }

  /**
   * getCoreField
   * @param string $strField
   * @return GenericElementField
   * @author Thomas Schedler <tsh@massiveart.com>
   * @version 1.0
   */
  public function getCoreField($strField){
    if(array_key_exists($strField, $this->arrCoreFields)){
      return $this->arrCoreFields[$strField];
    }
    return null;
  }

  /**
   * getFileField
   * @param string $strField
   * @return GenericElementField
   * @author Thomas Schedler <tsh@massiveart.com>
   * @version 1.0
   */
  public function getFileField($strField){
    if(array_key_exists($strField, $this->arrFileFields)){
      return $this->arrFileFields[$strField];
    }
    return null;
  }

  /**
   * getMultiField
   * @param string $strField
   * @return GenericElementField
   * @author Thomas Schedler <tsh@massiveart.com>
   * @version 1.0
   */
  public function getMultiField($strField){
    if(array_key_exists($strField, $this->arrMultiFields)){
      return $this->arrMultiFields[$strField];
    }
    return null;
  }

	/**
   * getInstanceField
   * @param string $strField
   * @return GenericElementField
   * @author Thomas Schedler <tsh@massiveart.com>
   * @version 1.0
   */
  public function getInstanceField($strField){
    if(array_key_exists($strField, $this->arrInstanceFields)){
      return $this->arrInstanceFields[$strField];
    }
    return null;
  }

  /**
   * getSpecialField
   * @param string $strField
   * @return GenericElementField
   * @author Thomas Schedler <tsh@massiveart.com>
   * @version 1.0
   */
  public function getSpecialField($strField){
    if(array_key_exists($strField, $this->arrSpecialFields)){
      return $this->arrSpecialFields[$strField];
    }
    return null;
  }

  /**
   * setFieldValues
   * @param array $arrValues
   * @author Thomas Schedler <tsh@massiveart.com>
   * @version 1.0
   */
  public function setFieldValues($arrValues){
    try{

      /**
       * go through the regions
       */
      foreach ($this->arrRegions as $objRegion) {

        if($objRegion->getRegionIsMultiply() == true){
          if(isset($arrValues['Region_'.$objRegion->getRegionId().'_Instances']) && isset($arrValues['Region_'.$objRegion->getRegionId().'_Order'])){
            /*$strRegionInstanceIds = trim($arrValues['Region_'.$objRegion->getRegionId().'_Instances'], '[]');
            $arrRegionInstanceIds = array();
            $arrRegionInstanceIds = split('\]\[', $strRegionInstanceIds);*/

            parse_str($arrValues['Region_'.$objRegion->getRegionId().'_Order'], $arrRegionOrder);

            /**
             * go through region instances
             */
            if(array_key_exists('divRegion_'.$objRegion->getRegionId(), $arrRegionOrder)){
              foreach($arrRegionOrder['divRegion_'.$objRegion->getRegionId()] as $intRegionInstanceId){
                if(is_numeric($intRegionInstanceId) && $intRegionInstanceId > 0){
                  $objRegion->addRegionInstanceId($intRegionInstanceId);

                  /**
                   * go through fields of the region
                   */
                  foreach ($objRegion->getFields() as $objField) {
                    if(array_key_exists($objField->name.'_'.$intRegionInstanceId, $arrValues)){
                      $objField->setInstanceValue($intRegionInstanceId, $arrValues[$objField->name.'_'.$intRegionInstanceId]);
                    }
                  }
                }
              }
            }
          }
        }else{
          /**
           * go through fields of the region
           */
          foreach ($objRegion->getFields() as $objField) {
            if(array_key_exists($objField->name, $arrValues)){
              $objField->setValue($arrValues[$objField->name]);
            }
          }
        }
      }

    }catch (Exception $exc) {
      $this->core->logger->err($exc);
    }
  }

	/**
	 * setMetaInformation
	 * @param Zend_Db_Table_Row $objCurrElement
	 * @author Thomas Schedler <tsh@massiveart.com>
	 * @version 1.0
	 */
	public function setMetaInformation(Zend_Db_Table_Row &$objCurrElement){

		if(count($objCurrElement) > 0){
			$this->setCreatorId($objCurrElement->creator);
			$this->setPublisherName($objCurrElement->publisher);
			$this->setChangeUserName($objCurrElement->changeUser);
		  $this->setShowInNavigation($objCurrElement->showInNavigation);
			if($objCurrElement->changed != '' || !is_null($objCurrElement->changed)){
				$this->setChangeDate($objCurrElement->changed);
			}
			if($objCurrElement->published != '' || !is_null($objCurrElement->published)){
				$this->setPublishDate($objCurrElement->published);
			}
			$this->setFormVersion($objCurrElement->version);
			$this->setStatusId($objCurrElement->idStatus);
		}
	}

  /**
   * getDataTypeObject
   * @param integer $intFormTypeId
   * @return GenericDataTypeAbstract
   * @author Thomas Schedler <tsh@massiveart.com>
   * @version 1.0
   */
  public static function getDataTypeObject($intFormTypeId){
    switch ($intFormTypeId){
      case GenericSetup::TYPE_PAGE :
        require_once(dirname(__FILE__).'/data/types/generic.data.type.page.class.php');
        return new GenericDataTypePage();
      case GenericSetup::TYPE_FOLDER :
        require_once(dirname(__FILE__).'/data/types/generic.data.type.folder.class.php');
        return new GenericDataTypeFolder();
      case GenericSetup::TYPE_CATEGORY :
        require_once(dirname(__FILE__).'/data/types/generic.data.type.category.class.php');
        return new GenericDataTypeCategory();
      case GenericSetup::TYPE_UNIT :
        require_once(dirname(__FILE__).'/data/types/generic.data.type.unit.class.php');
        return new GenericDataTypeUnit();
      case GenericSetup::TYPE_CONTACT :
        require_once(dirname(__FILE__).'/data/types/generic.data.type.contact.class.php');
        return new GenericDataTypeContact();
      case GenericSetup::TYPE_WIDGET:
      	require_once(dirname(__FILE__).'/data/types/generic.data.type.widget.class.php');
      	return new GenericDataTypeWidget();
      case GenericSetup::TYPE_SUBWIDGET:
      	require_once(dirname(__FILE__).'/data/types/generic.data.type.subwidget.class.php');
      	return new GenericDataTypeSubwidget();
    }
  }

  /**
   * getFormTypeHandle
   * @param integer $intFormTypeId
   * @return string
   * @author Thomas Schedler <tsh@massiveart.com>
   * @version 1.0
   */
  public static function getFormTypeHandle($intFormTypeId){
    switch ($intFormTypeId){
      case GenericSetup::TYPE_PAGE :
        return 'page';
      case GenericSetup::TYPE_FOLDER :
        return 'folder';
      case GenericSetup::TYPE_CATEGORY :
        return 'category';
      case GenericSetup::TYPE_WIDGET:
      	return 'widget';
      case GenericSetup::TYPE_SUBWIDGET:
      	return 'subwidget';
    }
  }

	/**
	 * getModelGenericForm
	 * @return Model_GenericForms
	 * @author Thomas Schedler <tsh@massiveart.com>
	 * @version 1.0
	 */
	public function getModelGenericForm(){
		if (null === $this->objModelGenericForm) {
			/**
			 * autoload only handles "library" compoennts.
			 * Since this is an application model, we need to require it
			 * from its modules path location.
			 */
			require_once GLOBAL_ROOT_PATH.$this->core->sysConfig->path->zoolu_modules.'core/models/GenericForms.php';
			$this->objModelGenericForm = new Model_GenericForms();
			$this->objModelGenericForm->setLanguageId($this->intFormLanguageId);
		}

		return $this->objModelGenericForm;
	}

  /**
   * getModelTemplates
   * @return Model_Templates
   * @author Thomas Schedler <tsh@massiveart.com>
   * @version 1.0
   */
  public function getModelTemplates(){
    if(null === $this->objModelTemplates){
      /**
       * autoload only handles "library" compoennts.
       * Since this is an application model, we need to require it
       * from its modules path location.
       */
      require_once GLOBAL_ROOT_PATH.$this->core->sysConfig->path->zoolu_modules.'cms/models/Templates.php';
      $this->objModelTemplates = new Model_Templates();
      $this->objModelTemplates->setLanguageId($this->intLanguageId);
    }
    return $this->objModelTemplates;
  }

	/**
	 * setGenFormId
	 * @param integer $intGenFormId
	 */
	public function setGenFormId($intGenFormId){
		$this->intGenFormId = $intGenFormId;
	}

	/**
	 * getGenFormId
	 * @param integer $intintGenFormId
	 */
	public function getGenFormId(){
		return $this->intGenFormId;
	}

	/**
	 * setFormId
	 * @param string $strFormId
	 */
	public function setFormId($strFormId){
		$this->strFormId = $strFormId;
	}

	/**
	 * getFormId
	 * @param string $strFormId
	 */
	public function getFormId(){
		return $this->strFormId;
	}

  /**
   * setTemplateId
   * @param integer $intTemplateId
   */
  public function setTemplateId($intTemplateId){
    $this->intTemplateId = $intTemplateId;
  }

  /**
   * getTemplateId
   * @param integer $intTemplateId
   */
  public function getTemplateId(){
    return $this->intTemplateId;
  }

	/**
	 * setFormVersion
	 * @param integer $intFormVersion
	 */
	public function setFormVersion($intFormVersion){
		$this->intFormVersion = $intFormVersion;
	}

	/**
	 * getFormVersion
	 * @param integer $intFormVersion
	 */
	public function getFormVersion(){
		return $this->intFormVersion;
	}

	/**
	 * setFormTitle
	 * @param string $strFormTitle
	 */
	public function setFormTitle($strFormTitle){
		$this->strFormTitle = $strFormTitle;
	}

	/**
	 * getFormTitle
	 * @param string $strFormTitle
	 */
	public function getFormTitle(){
		return $this->strFormTitle;
	}

	/**
	 * setFormTypeId
	 * @param integer $intFormTypeId
	 */
	public function setFormTypeId($intFormTypeId){
		$this->intFormTypeId = $intFormTypeId;
	}

	/**
	 * getFormTypeId
	 * @return integer $intFormTypeId
	 */
	public function getFormTypeId(){
		return $this->intFormTypeId;
	}

  /**
   * setFormType
   * @param string $strFormType
   */
  public function setFormType($strFormType){
    $this->strFormType = $strFormType;
  }

  /**
   * getFormType
   * @return string $strFormType
   */
  public function getFormType(){
    return $this->strFormType;
  }

	/**
	 * setActionType
	 * @param integer $intActionType
	 */
	public function setActionType($intActionType){
		$this->intActionType = $intActionType;
	}

	/**
	 * getActionType
	 * @param integer $intActionType
	 */
	public function getActionType(){
		return $this->intActionType;
	}

	/**
	 * setRegionId
	 * @param integer $intRegionId
	 */
	public function setRegionId($intRegionId){
		$this->intRegionId = $intRegionId;
	}
	
	/**
	 * setWidgetInstanceId
	 * @param integer $intWidgetInstanceId
	 */
	public function setWidgetInstanceId($intWidgetInstanceId){
		$this->intWidgetInstanceId = $intWidgetInstanceId;
	}

	/**
	 * getWidgetInstanceId
	 */
	public function getWidgetInstanceId(){
		return $this->intWidgetInstanceId;
	}
	
	/**
	 * setSubwidgetId
	 * @param string $strSubwidgetId
	 */
	public function setSubwidgetId($strSubwidgetId){
		$this->strSubwidgetId = $strSubwidgetId;
	}
	
	/**
	 * getSubwidgetId
	 * @return string
	 */
	public function getSubwidgetId(){
		return $this->strSubwidgetId; 
	}
	
	/**
	 * getRegionId
	 * @param integer $intRegionId
	 */
	public function getRegionId(){
		return $this->intRegionId;
	}

	/**
	 * setRegionCols
	 * @param integer $intRegionCols
	 */
	public function setRegionCols($intRegionCols){
		$this->intRegionCols = $intRegionCols;
	}

	/**
	 * getRegionCols
	 * @param integer $intRegionCols
	 */
	public function getRegionCols(){
		return $this->intRegionCols;
	}

	/**
	 * setRegionPosition
	 * @param integer $intRegionPosition
	 */
	public function setRegionPosition($intRegionPosition){
		$this->intRegionPosition = $intRegionPosition;
	}

	/**
	 * getRegionPosition
	 * @param integer $intRegionPosition
	 */
	public function getRegionPosition(){
		return $this->intRegionPosition;
	}

	/**
	 * setRegionTitle
	 * @param string $strRegionTitle
	 */
	public function setRegionTitle($strRegionTitle){
		$this->strRegionTitle = $strRegionTitle;
	}

	/**
	 * getRegionTitle
	 * @param string $strRegionTitle
	 */
	public function getRegionTitle(){
		return $this->strRegionTitle;
	}

	/**
	 * setRegionCollapsable
	 * @param boolean $blnRegionCollapsable
	 */
	public function setRegionCollapsable($blnRegionCollapsable){
		$this->blnRegionCollapsable = $blnRegionCollapsable;
	}

	/**
	 * getRegionCollapsable
	 * @param boolean $blnRegionCollapsable
	 */
	public function getRegionCollapsable(){
		return $this->blnRegionCollapsable;
	}

	/**
	 * setLanguageId
	 * @param integer $intLanguageId
	 */
	public function setLanguageId($intLanguageId){
		$this->intLanguageId = $intLanguageId;
	}

	/**
	 * getLanguageId
	 * @param integer $intLanguageId
	 */
	public function getLanguageId(){
		return $this->intLanguageId;
	}

  /**
   * setFormLanguageId
   * @param integer $intFormLanguageId
   */
  public function setFormLanguageId($intFormLanguageId){
    $this->intFormLanguageId = $intFormLanguageId;
  }

  /**
   * getFormLanguageId
   * @param integer $intFormLanguageId
   */
  public function getFormLanguageId(){
    return $this->intFormLanguageId;
  }

	/**
	 * setElementId
	 * @param integer $intElementId
	 */
	public function setElementId($intElementId){
		$this->intElementId = $intElementId;
	}

	/**
	 * getElementId
	 * @param integer $intElementId
	 */
	public function getElementId(){
		return $this->intElementId;
	}

	/**
	 * setParentId
	 * @param integer $intParentId
	 */
	public function setParentId($intParentId){
		$this->intParentId = $intParentId;
	}

	/**
	 * getParentId
	 * @param integer $intParentId
	 */
	public function getParentId(){
		return $this->intParentId;
	}

	/**
	 * setParentTypeId
	 * @param integer $intParentTypeId
	 */
	public function setParentTypeId($intParentTypeId){
		$this->intParentTypeId = $intParentTypeId;
	}

	/**
	 * getParentTypeId
	 * @param integer $intParentTypeId
	 */
	public function getParentTypeId(){
		return $this->intParentTypeId;
	}

	/**
	 * setRootLevelId
	 * @param integer $intRootLevelId
	 */
	public function setRootLevelId($intRootLevelId){
		$this->intRootLevelId = $intRootLevelId;
	}

	/**
	 * getRootLevelId
	 * @param integer $intRootLevelId
	 */
	public function getRootLevelId(){
		return $this->intRootLevelId;
	}

  /**
   * setRootLevelTypeId
   * @param integer $intRootLevelTypeId
   */
  public function setRootLevelTypeId($intRootLevelTypeId){
    $this->intRootLevelTypeId = $intRootLevelTypeId;
  }

  /**
   * getRootLevelTypeId
   * @param integer $intRootLevelTypeId
   */
  public function getRootLevelTypeId(){
    return $this->intRootLevelTypeId;
  }

	/**
	 * setElementTypeId
	 * @param integer $intElementTypeId
	 */
	public function setElementTypeId($intElementTypeId){
		$this->intElementTypeId = $intElementTypeId;
	}

	/**
	 * getElementTypeId
	 * @param integer $intElementTypeId
	 */
	public function getElementTypeId(){
		return $this->intElementTypeId;
	}

	//FIXME change more than only methodsname!
  /**
   * setIsStartElement
   * @param boolean $blnIsStartPage
   */
  public function setIsStartElement($blnIsStartPage, $blnValidate = true){
    if($blnValidate == true){
      if($blnIsStartPage === true || $blnIsStartPage === 'true' || $blnIsStartPage == 1){
        $this->blnIsStartPage = true;
      }else{
        $this->blnIsStartPage = false;
      }
    }else{
      $this->blnIsStartPage = $blnIsStartPage;
    }
  }

  /**
   * getIsStartElement
   * @return boolean $blnIsStartPage
   */
  public function getIsStartElement($blnReturnAsNumber = true){
    if($blnReturnAsNumber == true){
      if($this->blnIsStartPage == true){
        return 1;
      }else{
        return 0;
      }
    }else{
      return $this->blnIsStartPage;
    }
  }

  /**
   * setShowInNavigation
   * @param boolean $blnShowInNavigation
   */
  public function setShowInNavigation($blnShowInNavigation, $blnValidate = true){
    if($blnValidate == true){
      if($blnShowInNavigation === true || $blnShowInNavigation === 'true' || $blnShowInNavigation == 1){
        $this->blnShowInNavigation = true;
      }else{
        $this->blnShowInNavigation = false;
      }
    }else{
      $this->blnShowInNavigation = $blnShowInNavigation;
    }
  }

  /**
   * getShowInNavigation
   * @return boolean $blnShowInNavigation
   */
  public function getShowInNavigation($blnReturnAsNumber = true){
    if($blnReturnAsNumber == true){
      if($this->blnShowInNavigation == true){
        return 1;
      }else{
        return 0;
      }
    }else{
      return $this->blnShowInNavigation;
    }
  }

	/**
	 * setCreatorId
	 * @param integer $intCreatorId
	 */
	public function setCreatorId($intCreatorId){
		$this->intCreatorId = $intCreatorId;
	}

	/**
	 * getCreatorId
	 * @param integer $intCreatorId
	 */
	public function getCreatorId(){
		return $this->intCreatorId;
	}

	/**
	 * setPublisherName
	 * @param string $strPublisherName
	 */
	public function setPublisherName($strPublisherName){
		$this->strPublisherName = $strPublisherName;
	}

	/**
	 * getPublisherName
	 * @param string $strPublisherName
	 */
	public function getPublisherName(){
		return $this->strPublisherName;
	}

	/**
	 * setChangeUserName
	 * @param string $strChangeUserName
	 */
	public function setChangeUserName($strChangeUserName){
		$this->strChangeUserName = $strChangeUserName;
	}

	/**
	 * getChangeUserName
	 * @param string $strChangeUserName
	 */
	public function getChangeUserName(){
		return $this->strChangeUserName;
	}

	/**
	 * setStatusId
	 * @param integer $intStatusId
	 */
	public function setStatusId($intStatusId){
		$this->intStatusId = $intStatusId;
	}

	/**
	 * getStatusId
	 * @param integer $intStatusId
	 */
	public function getStatusId(){
		return $this->intStatusId;
	}

  /**
   * setUrlFolder
   * @param integer $intUrlFolder
   */
  public function setUrlFolder($intUrlFolder){
    $this->intUrlFolder = $intUrlFolder;
  }

  /**
   * getUrlFolder
   * @param integer $intUrlFolder
   */
  public function getUrlFolder(){
    return $this->intUrlFolder;
  }

	/**
	 * setChangeDate
	 * @param string/obj $Date
	 * @author Thomas Schedler <tsh@massiveart.com>
	 * @version 1.0
	 */
	public function setChangeDate($Date, $blnIsValidDateObj = false){
		if($blnIsValidDateObj == true){
			$this->objChangeDate = $Date;
		}else{
			$arrTmpTimeStamp = explode(' ', $Date);
			if(count($arrTmpTimeStamp) > 0){
				$arrTmpTime = explode(':', $arrTmpTimeStamp[1]);
				$arrTmpDate = explode('-', $arrTmpTimeStamp[0]);
				if(count($arrTmpDate) == 3){
					$this->objChangeDate =  mktime($arrTmpTime[0], $arrTmpTime[1], $arrTmpTime[2], $arrTmpDate[1], $arrTmpDate[2], $arrTmpDate[0]);
				}
			}
		}
	}

	/**
	 * getChangeDate
	 * @param string $strFormat
	 * @return string $strChangeDate
	 * @author Thomas Schedler <tsh@massiveart.com>
	 * @version 1.0
	 */
	public function getChangeDate($strFormat = 'Y-m-d', $blnGetDateObj = false){
		if($blnGetDateObj == true){
			return $this->objChangeDate;
		}else{
			if($this->objChangeDate != null){
				return date($strFormat, $this->objChangeDate);
			}else{
				return null;
			}
		}
	}

	/**
	 * setPublishDate
	 * @param string/obj $Date
	 * @author Thomas Schedler <tsh@massiveart.com>
	 * @version 1.0
	 */
	public function setPublishDate($Date, $blnIsValidDateObj = false){
		if($blnIsValidDateObj == true){
			$this->objPublishDate = $Date;
		}else{
			$arrTmpTimeStamp = explode(' ', $Date);
			if(count($arrTmpTimeStamp) > 0){
				$arrTmpTime = explode(':', $arrTmpTimeStamp[1]);
				$arrTmpDate = explode('-', $arrTmpTimeStamp[0]);
				if(count($arrTmpDate) == 3){
					$this->objPublishDate =  mktime($arrTmpTime[0], $arrTmpTime[1], $arrTmpTime[2], $arrTmpDate[1], $arrTmpDate[2], $arrTmpDate[0]);
				}
			}
		}
	}

	/**
	 * getPublishDate
	 * @param string $strFormat
	 * @return string $strPublishDate
	 * @author Thomas Schedler <tsh@massiveart.com>
	 * @version 1.0
	 */
	public function getPublishDate($strFormat = 'Y-m-d H:i:s', $blnGetDateObj = false){
		if($blnGetDateObj == true){
			return $this->objPublishDate;
		}else{
			if($this->objPublishDate != null){
				return date($strFormat, $this->objPublishDate);
			}else{
				return null;
			}
		}
	}

	/**
   * setModelSubPath
   * @param string $strModelSubPath
   */
  public function setModelSubPath($strModelSubPath){
    $this->strModelSubPath = $strModelSubPath;
  }

  /**
   * getModelSubPath
   * @param string $strModelSubPath
   */
  public function getModelSubPath(){
    return $this->strModelSubPath;
  }
}

?>